package com.xli.module.framemodule.controller;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xli.dto.component.SelectItem;
import com.xli.dto.component.TreeNode;
import com.xli.dto.component.model.TableModel;
import com.xli.dto.component.model.TreeModel;
import com.xli.dto.component.util.TreeUtil;
import com.xli.dto.controller.BaseController;
import com.xli.dto.result.ResultVO;
import com.xli.dto.result.status.Status;
import com.xli.dto.validation.group.IGroup;
import com.xli.mis.codemain.utils.CodeModalFactory;
import com.xli.module.framemodule.entity.dto.ModuleAddDTO;
import com.xli.module.framemodule.entity.dto.ModuleSearchDTO;
import com.xli.module.framemodule.entity.dto.ModuleUpdateDTO;
import com.xli.module.framemodule.entity.mapper.IFrameModuleMapper;
import com.xli.module.framemodule.entity.vo.ModuleVO;
import com.xli.ui.module.framemodule.entity.FrameModule;
import com.xli.ui.module.framemodule.service.IFrameModuleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Tag(name = "模块管理")
@RestController
@RequestMapping("/frameModule")
public class FrameModuleController extends BaseController {

    @Autowired
    IFrameModuleService iFrameModuleService;

    @Operation(summary = "新增模块")
    @PostMapping(value = "/add")
    public ResultVO<String> add(@RequestBody @Validated(IGroup.add.class) ModuleAddDTO dto) {
        FrameModule frameModule = IFrameModuleMapper.INSTANCE.toEntity(dto);
        frameModule.setId(IdUtil.getSnowflakeNextId());
        if (iFrameModuleService.insert(frameModule)) {
            return new ResultVO<>(Status.SUCCESS, "添加成功");
        }
        return new ResultVO<>(Status.FAILED, "添加失败");
    }

    @Operation(summary = "删除模块")
    @PostMapping(value = "/delete")
    public ResultVO<String> delete(@RequestBody @NotEmpty(message = "id不能为空") List<String> ids) {
        int i = 0;
        for (String id : ids) {
            if (iFrameModuleService.delete(Long.valueOf(id))) {
                i++;
            }
        }
        return new ResultVO<>(Status.SUCCESS, i + "条数据删除成功");
    }

    @Operation(summary = "更新模块")
    @PostMapping(value = "/update")
    public ResultVO<String> update(@RequestBody @Validated(IGroup.update.class) ModuleUpdateDTO dto) {
        FrameModule frameModule = IFrameModuleMapper.INSTANCE.toEntity(dto);
        if (iFrameModuleService.update(frameModule)) {
            return new ResultVO<>(Status.SUCCESS, "更新成功");
        }
        return new ResultVO<>(Status.FAILED, "更新失败");
    }

    @Operation(summary = "查询详情")
    @PostMapping(value = "/detail/{id}")
    public ResultVO<ModuleVO> detail(@PathVariable("id") @NotBlank(message = "id必填") String id) {
        FrameModule frameModule = iFrameModuleService.find(Long.valueOf(id));
        if (frameModule != null) {
            return new ResultVO<>(Status.SUCCESS, "查询成功", IFrameModuleMapper.INSTANCE.toVO(frameModule));
        }
        return new ResultVO<>(Status.FAILED, "查询失败");
    }

    @Operation(summary = "查询列表")
    @PostMapping(value = "/search")
    public ResultVO<TableModel<ModuleVO, FrameModule, ModuleSearchDTO>> search(@RequestBody @Validated(IGroup.search.class) ModuleSearchDTO dto) {

        TableModel<ModuleVO, FrameModule, ModuleSearchDTO> tableModel = new TableModel<>(dto) {

            @Override
            public List<ModuleVO> fetch(int current, int pageSize) {
                Page<FrameModule> page = iFrameModuleService.findModuleListByPid(Long.valueOf(dto.getPid()), dto.getType(), dto.getName(), current, pageSize);
                this.setTotal(page.getTotal());
                return page.convert(IFrameModuleMapper.INSTANCE::toVO).getRecords();
            }
        };
        return new ResultVO<>(Status.SUCCESS, "查询成功", tableModel);
    }

    @Operation(summary = "查询模块层级")
    @PostMapping(value = "/fetchTree")
    public ResultVO<TreeModel<ModuleVO>> fetchTree() {

        TreeModel<ModuleVO> treeModel = new TreeModel<>() {

            @Override
            public List<TreeNode<ModuleVO>> fetch() {
                return getTreeNodeList(TreeUtil.defaultPid);
            }

            @Override
            public List<TreeNode<ModuleVO>> fetchChild(TreeNode<ModuleVO> treeNode) {
                return getTreeNodeList(treeNode.getId());
            }

            private List<TreeNode<ModuleVO>> getTreeNodeList(Long pid) {
                List<FrameModule> frameModuleList = iFrameModuleService.findModuleListByPid(pid);
                return frameModuleList.stream()
                        .filter(frameModule -> "0".equals(frameModule.getType()))
                        .map(frameModule -> {
                            TreeNode<ModuleVO> treeNode = new TreeNode<>();
                            treeNode.setId(frameModule.getId());
                            treeNode.setPid(frameModule.getPid());
                            treeNode.setText(frameModule.getName());
                            treeNode.setVo(IFrameModuleMapper.INSTANCE.toVO(frameModule));
                            return treeNode;
                        }).collect(Collectors.toList());
            }
        };
        return new ResultVO<>(Status.SUCCESS, "查询成功", treeModel);
    }

    @Operation(summary = "模块显隐")
    @PostMapping(value = "/getIsShowModel")
    public ResultVO<List<SelectItem>> getIsShowModel() {
        List<SelectItem> selectItemList = CodeModalFactory.factory("模块显隐", false);
        return new ResultVO<>(Status.SUCCESS, "查询成功", selectItemList);
    }

    @Operation(summary = "模块状态")
    @PostMapping(value = "/getStateModel")
    public ResultVO<List<SelectItem>> getStateModel() {
        List<SelectItem> selectItemList = CodeModalFactory.factory("模块状态", false);
        return new ResultVO<>(Status.SUCCESS, "查询成功", selectItemList);
    }

    @Operation(summary = "模块类型")
    @PostMapping(value = "/getTypeModel")
    public ResultVO<List<SelectItem>> getTypeModel() {
        List<SelectItem> selectItemList = CodeModalFactory.factory("模块类型", false);
        return new ResultVO<>(Status.SUCCESS, "查询成功", selectItemList);
    }
}
